#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Aug 15 16:07:40 2018

@author: jia.liu
"""
import pandas as pd

def my_df_compare(df1, df2, sort_key):
    '''
    primary_key: 行排序的依据主键，可为字符串或列表
    '''
    li_diff = []
    diff_flag = 0
    df1 = df1.sort_index(axis = 1).sort_index(axis = 0,by = sort_key).reset_index(drop=True)
    df2 = df2.sort_index(axis = 1).sort_index(axis = 0,by = sort_key).reset_index(drop=True)
    col1 = df1.columns
    col2 = df2.columns
    if not (col1 == col2).all():
        print('列名不一致', col1[(col1 !=col2)])
        return col1[(col1 !=col2)]
    
    for i in df1.columns.tolist():
        if (df1[i] == df2[i]).all():
            pass
        else:
            diff_flag = 1
            try:
                if (df1[i].astype(int) - df2[i].astype(int)).sum() != 0:
                    print('差异的列：',i)
                    df0 = pd.DataFrame()
                    df0['df1'] = df1[i].astype(int)
                    df0['df2'] = df2[i].astype(int)
    
                    diff = pd.DataFrame()
                    diff['df1'] = df0['df1'][(df0['df1'] - df0['df2'])!=0]
                    diff['df2'] = df0['df2'][(df0['df1'] - df0['df2'])!=0]
                    print(diff[['df1','df2']])
                    print('-'*50)
                li_diff.append(i)   
            except Exception:
                print('差异的列（非数字）：',i)
    if diff_flag == 0:
        print('-'*50)
        print('结果一致')
        print('-'*50)
#df1.amtp_wsum[433]
#df2.amtp_wsum[433]

if __name__ == '__main__':
    df1 = df_result_his
    df2 = df_result_his2
